package com.yunshu.yunshuapp.tongxunlu;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yunshu.yunshuapp.NavigationBar;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.WebviewActivity;
import com.yunshu.yunshuapp.network.YSCallback;
import com.yunshu.yunshuapp.network.YSRequest;
import com.yunshu.yunshuapp.user.YSUser;
import com.yunshu.yunshuapp.utils.YSUtils;

import java.io.IOException;

public class ContactDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private YSRequest request;
    private Contact model;
    private String contactId;
    private ImageLoader mImageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);
        request = new YSRequest(this);
        model = new Contact();
        NavigationBar bar = (NavigationBar) findViewById(R.id.navigationBar);
        bar.putInActivity(this);
        bar.setTitle("厂家详情");

        YSUtils.initImageLoader(this);

        // 获取图片加载实例
        mImageLoader = ImageLoader.getInstance();

        Bundle bundle = this.getIntent().getExtras();
        contactId = (String) bundle.get("contactId");

        this.refresh();

        this.addListener();
    }

    private void addListener() {
        View productListV = findViewById(R.id.productList);
        productListV.setOnClickListener(this);
        View priceV = findViewById(R.id.contacts);
        priceV.setOnClickListener(this);
//        View phoneV = findViewById(R.id.phone);
//        phoneV.setOnClickListener(this);
        View website = findViewById(R.id.buttonURL);
        website.setOnClickListener(this);

    }

    private void refresh() {
        request.GET(YSRequest.TONGXUNLU_DETAIL, "id="+contactId, new YSCallback() {
            @Override
            public void onResponse(int isSuccess, JsonNode result) {
                if (isSuccess != YSRequest.REQUEST_OK){
                    return;
                }
//                Iterator<JsonNode> iterator = result.iterator();
                ObjectMapper mapper = new ObjectMapper();
                String jsonstr = result.toString();
                try {
                    model = mapper.readValue(jsonstr, Contact.class);
                    reloadData();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void reloadData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ImageView logoImgView = (ImageView) findViewById(R.id.logoImgView);
                if (model.getLogo()!= null) {
                    logoImgView.setBackgroundColor(0xf5f5f5);
                    mImageLoader.displayImage(model.getLogo(), logoImgView);
                }

                TextView titleTV = (TextView) findViewById(R.id.textView);
                titleTV.setText(model.getName());

                TextView descriptionTV = (TextView) findViewById(R.id.descriptionTV);
                descriptionTV.setText(model.getBriefing());

            }
        });

    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.productList ){
            Intent intent = new Intent();
            intent.setClass(this,WebviewActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("url",this.productURL());
            bundle.putString("title","产品列表");
            intent.putExtras(bundle);
            startActivityForResult(intent,0);
        }else if(view.getId() == R.id.contacts){
            Intent intent = new Intent();
            intent.setClass(this,WebviewActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("url",this.priceStrategyURL());
            bundle.putString("title","联系方式");
            intent.putExtras(bundle);
            startActivityForResult(intent,0);
        }else if(view.getId() == R.id.buttonURL){
            Intent intent = new Intent();
            intent.setClass(this,WebviewActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("title","官方网站");
            bundle.putString("url",this.model.getOfficialWebsite());
            intent.putExtras(bundle);
            startActivityForResult(intent,0);
        }
    }

    private String priceStrategyURL() {
        return YSRequest.BASE_URL + YSRequest.TONGXUNLU_DETAIL_PRICE+"&id="+model.getContactId()+"&token="+ YSUser.instance().getToken();
    }

    public String productURL() {
        return YSRequest.BASE_URL + YSRequest.TONGXUNLU_DETAIL_PRODUCT+"&id="+model.getContactId()+"&token="+ YSUser.instance().getToken();
    }
}
